"""
        爬取猫眼电影网的top100的电影信息
"""
import json
import requests
from requests.exceptions import RequestException
import re


def fw(url):
    try:
        r=requests.get(url)
        if r.status_code == 200:
            return r.text
        return None
    except RequestException:
        return None


def hq(html):
    """
        获取想要爬取内容
    """
    pen = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?img src="(.*?)".*?alt="(.*?)".*?board-img.*?'
                   +'src=".*?".*?star">(.*?)</p>.*?relesetime">(.*?)</p>.*?integer">(.*?)</i>.*?'
                    +'fraction">(.*?)</i>.*?</dd>', re.S)
    items = re.findall(pen, html)
    for i in items:
        yield {
            'paiming': i[0],
            'tupiam': i[1],
            'mingcheng': i[2],
            'zhuyan': i[3].strip()[3:],
            'shijian': i[4].strip()[5:],
            'pingfen': i[5]+i[6]
        }


def cc(sj):
    """
    将爬取的内容存储成TET形式文本
    """
    with open('top100.txt', 'a')as f:
        f.write(json.dumps(sj)+'\n')
        f.close()


def main():
    url = "https://maoyan.com/board/4"
    html = fw(url)
    for li in hq(html):
        print(li)
        cc(li)


if __name__ == "name":
    main()
